using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneTransition : MonoBehaviour
{
    public GameObject canvas;
    public Animator animator;
    public float duration = 1;


    public static SceneTransition Instance { get; set; }

    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            canvas.SetActive(true);
            DontDestroyOnLoad(this.gameObject);
        } else
        {
            Destroy(gameObject);
        }
    }

    public void LoadScene(string sceneName)
    {
        StartCoroutine(DoLoadScene(sceneName));
    }

    IEnumerator DoLoadScene(string sceneName)
    {
        animator.SetTrigger("FadeIn");
        yield return new WaitForSeconds(duration);
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneName);
        asyncOperation.completed += OnLoadSceneCompleted;
    }

    private void OnLoadSceneCompleted(AsyncOperation obj)
    {
        animator.SetTrigger("FadeOut");
    }
}
